package spzx.controller;

import com.atguigu.spzx.model.dto.system.LoginDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.system.LoginVo;
import com.atguigu.spzx.model.vo.system.ValidateCodeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import spzx.exception.SpzxException;
import spzx.service.SysUserService;

import java.util.List;

//@CrossOrigin        //借助此注解可以解决跨域问题，但是治标不治本，建议用配置类解决跨域问题
@Tag(description = "后台系统用户管理相关接口",name = "系统用户管理")
@RestController
@RequestMapping("admin/sysUser")
//@CrossOrigin
public class SysUserController {
    @Autowired
    SysUserService sysUserService;

    @GetMapping("getValidateCode")
    @Operation(summary = "获取图形验证码")
    public Result getValidateCode() {
        ValidateCodeVo validateCodeVo = sysUserService.getValidateCode();
        return Result.build(validateCodeVo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "退出端口")
    @GetMapping("logout")
    public Result logout(@RequestHeader String token) {
        sysUserService.logout(token);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "根据前端请求的token获取用户信息")
//    @GetMapping("userInfo")
    @GetMapping("userinfo")
    public Result userInfo(@RequestHeader(required = true) String token) {
        System.out.println("controller--> preHandle方法-->" + Thread.currentThread().getName());
        //根据token获取用户信息
        SysUser sysUser = sysUserService.userInfo(token);
        return Result.build(sysUser, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "后台系统用户登录接口")
    @PostMapping("/index/login")
    public Result login(@RequestBody LoginDto loginDto) {
        //loginVo={token:xxx}
        LoginVo loginVo = sysUserService.login(loginDto);

        //weisha fanhui nll a >???
        return Result.build(loginVo,ResultCodeEnum.SUCCESS);
        //{code:200,msg:"xxx",data:{token:xxx}}
    }


    @Operation(description = "查询所有系统用户（描述）", summary = "查询所有系统用户")
    @GetMapping("sysUser/findAll")
    public Result findAll() {
        List<SysUser> list = sysUserService.findAll();
        if(list.isEmpty()){
            throw new SpzxException(9000,"用户列表为空");
        }

//        return Result.build(list,200, "success");
        return Result.build(list, ResultCodeEnum.SUCCESS);
    }
}
